package com.foxitcloud.webpdf.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author shaojie_zhuo
 * @version 1.0 File utils.
 */
public final class FXFileUtils {

	private static final Logger log = Logger.getLogger(FXFileUtils.class);

	public static boolean createDir(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return file.mkdirs();
		}
		return true;
	}
	/**
	 * Write data to an new file.
	 * 
	 * @param filePath
	 *            file path.
	 * @param inputData
	 *            data to write.
	 * @param bCreateNew
	 *            whether create an new file when the file does not exist.
	 * @param bTruncate
	 *            whether to truncate.
	 * @return return true if write data successfully, otherwise return false.
	 */
	public static boolean writeDataToFile(String filePath, byte[] inputData,
			boolean bCreateNew, boolean bTruncate) {
		if (filePath == null || filePath.equalsIgnoreCase("")
				|| inputData == null)
			return false;
		
		File file = new File(filePath);
		String fileDir = file.getParent();
		if (!StringUtils.isEmpty(fileDir) && !createDir(fileDir)) {
			return false;
		}
		
		FileOutputStream fs = null;
		try {
			if (!file.exists()) {
				if (!bCreateNew)
					return false;
				fs = new FileOutputStream(filePath, false);
			} else {
				if (bTruncate) {
					fs = new FileOutputStream(filePath, false);
				} else {
					fs = new FileOutputStream(filePath, true);
				}
			}
			fs.write(inputData, 0, inputData.length);
			fs.flush();
		} catch (Exception ex) {
			return false;
		} finally {
			if (fs != null) {
				try {
					fs.close();
				} catch (IOException e) {
					log.warn(e);
				}
			}
		}
		return true;
	}

	/**
	 * Read data from file.
	 * 
	 * @param filename
	 *            file path.
	 * @return data of file.
	 * @throws IOException
	 *             IO exception.
	 */
	public static byte[] readToByteArray(String filename) throws IOException {

		File f = new File(filename);
		if (!f.exists()) {
			throw new FileNotFoundException(filename);
		}

		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(f));
			int buf_size = 1024;
			byte[] buffer = new byte[buf_size];
			int len = 0;
			while (-1 != (len = in.read(buffer, 0, buf_size))) {
				bos.write(buffer, 0, len);
			}
			return bos.toByteArray();
		} catch (IOException e) {
			log.warn(e);
			throw e;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				log.warn(e);
			}
			bos.close();
		}
	}

	/**
	 * Read data from an InputStream.
	 * 
	 * @param fileInputStream
	 *            InputStream to read.
	 * @return data of InputStream.
	 * @throws IOException
	 *             IO exception.
	 */
	public static byte[] readToByteArray(InputStream fileInputStream)
			throws IOException {
		if (fileInputStream == null)
			return null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream(
				fileInputStream.available());
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(fileInputStream);
			int buf_size = 1024;
			byte[] buffer = new byte[buf_size];
			int len = 0;
			while (-1 != (len = in.read(buffer, 0, buf_size))) {
				bos.write(buffer, 0, len);
			}
			return bos.toByteArray();
		} catch (IOException e) {
			log.warn(e);
			return null;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				log.warn(e);
			}
			bos.close();
		}
	}
}
